//
//  CreateSignUpTableController.m
//  CommunityNannyO2O
//
//  Created by 90 on 15/7/21.
//  Copyright (c) 2015年 Apple. All rights reserved.
//

#import "CreateSignUpTableController.h"
#import "AppConfig.h"
#import "QXNetWorkTool.h"
#import "APIAdress.h"
#import "MBProgressHUD+YC.h"
#import "MJExtension.h"
#import "AccountTool.h"
#import "ExamSignUpTableViewCellFirst.h"
#import "ExamSignUpTableViewCellSecond.h"
#import "ExamSignUpTableViewCellThird.h"
#import "ExamSignUpTableViewCellFouth.h"
#import "ExamSignUpTableViewCellFirth.h"
#import "ExamSignUpTableViewCellSix.h"
#import "SelectExamLevelView.h"
#import "ExamGuideViewController.h"
#import "ExamCourseViewController.h"
#import "PrepareGuideViewController.h"
#import "UserDataSingleton.h"
#import "GetSignUpCostModel.h"
#import "SignUpSuccessModel.h"
#import "MyTestViewController.h"

@interface CreateSignUpTableController()<QXNetWorkToolDelegate,
        UITableViewDataSource,UITableViewDelegate,
        LevelDeletage,SelectExamLevelDeletage,
        CourseDeletage,SelectExamCourseViewDeletage,
        ExamGuideTableViewCellDeletage, SignUpDeletage>
{

}

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (copy, nonatomic) NSString *cost;
@property (copy, nonatomic) NSString *exam_id;
@property (strong, nonatomic) ExamSignUpTableViewCellSecond *cellSecond;
@end


@implementation CreateSignUpTableController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView.dataSource = self;
    
    self.tableView.delegate = self;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
 
    [MBProgressHUD showMessage:@"正在加载"];
    
    QXNetWorkTool *QXnetWorkTool = [[QXNetWorkTool alloc]init];
    
    QXnetWorkTool.QXNetWorkDelegate = self;
    
    [QXnetWorkTool GetDataFromServerWithPostMethodandInterfaceName:GetSigeUpCostInterface andParameter:
     @[@{@"major_id":[UserDataSingleton shareInstance].examCourseId,
         @"grade_num":[UserDataSingleton shareInstance].examLevel}] tag:1];
}

- (void)GetFailResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUD];
    
    if (Response == nil)
        return;
    
    NSString *message = Response;
    
    [MBProgressHUD showError:message];
}

- (void)GetSuccessMessage:(id)message andTag:(int)tag
{
    [MBProgressHUD hideHUD];
    
    NSString *mes = message;
    
    switch (tag)
    {
        case 2:
            [MBProgressHUD showSuccess:mes];
            
            break;
    }
}

-(void)GetSuccessResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUD];
    if (Response == nil)
    {
        return;
    }
    switch (tag)
    {
        case 1:
        {
            for (NSDictionary *dic in Response)
            {
                GetSignUpCostModel *model = [GetSignUpCostModel GetSignUpCostWithDict:dic];
                self.cost = model.cost;
                
                if (self.tableView)
                {
                    [self.tableView reloadData];
                }
                break;
            }
            break;
        }
        case 2:
        {
            for (NSDictionary *dic in Response)
            {
                SignUpSuccessModel *model = [SignUpSuccessModel SignUpSuccessWithDict:dic];
                
                self.exam_id = model.exam_id;
                [UserDataSingleton shareInstance].exam_id = model.exam_id;
                [UserDataSingleton shareInstance].isSelfInfoChange = YES;
                
                break;
            }
            
            MyTestViewController *obj = [MyTestViewController create];
            obj.isReturnTOShouYe = YES;
            
            [self.navigationController pushViewController:obj animated:YES];
            
            break;
        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)         //first cell
    {
        return 191;
    }
    else if (indexPath.row == 1)    //second cell
    {
        return [self.cellSecond  getHeight];
    }
    else if (indexPath.row == 2)    //thirth cell
    {
        return 91;
    }
    else if (indexPath.row == 3)    //fourth cell
    {
        return 75;
    }
    else if (indexPath.row == 4)    //firth cell
    {
        return 85;
    }
    else if (indexPath.row == 5)
    {
        return 108;
    }
    return 50;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0)
    {
        ExamSignUpTableViewCellFirst *cell = [ExamSignUpTableViewCellFirst cellWithTableView:tableView];
        
        cell.yearLabel.text = [NSString stringWithFormat:@"%@年", [UserDataSingleton shareInstance].year];
        
        //月日时分秒
        NSString *tmpMonth = [UserDataSingleton shareInstance].month;
        NSString *tmpDay = [UserDataSingleton shareInstance].day;
        NSString *tmpShour = [UserDataSingleton shareInstance].sHour;
        NSString *tmpSminute = [UserDataSingleton shareInstance].sMinute;
        NSString *tmpEhour = [UserDataSingleton shareInstance].eHour;
        NSString *tmpEminute = [UserDataSingleton shareInstance].eMinute;
        
        if ([tmpMonth intValue] < 10)
        {
            tmpMonth = [NSString stringWithFormat:@"0%@",
                        [UserDataSingleton shareInstance].month];
        }
        
        if ([tmpDay intValue] < 10)
        {
            tmpDay = [NSString stringWithFormat:@"0%@",
                      [UserDataSingleton shareInstance].day];
        }
        
        if ([tmpShour intValue] < 10)
        {
            tmpShour = [NSString stringWithFormat:@"0%@",
                        [UserDataSingleton shareInstance].sHour];
        }
        
        if ([tmpSminute intValue] < 10)
        {
            tmpSminute = [NSString stringWithFormat:@"0%@",
                          [UserDataSingleton shareInstance].sMinute];
        }
        
        if ([tmpEhour intValue] < 10)
        {
            tmpEhour = [NSString stringWithFormat:@"0%@",
                        [UserDataSingleton shareInstance].eHour];
        }
        
        if ([tmpEminute intValue] < 10)
        {
            tmpEminute = [NSString stringWithFormat:@"0%@",
                          [UserDataSingleton shareInstance].eMinute];
        }
        
        //月份
        cell.monthAndDayLabel.text =
                [NSString stringWithFormat:@"%@月%@日", tmpMonth, tmpDay];

        
        //时间
        NSString *time = [NSString stringWithFormat:@"%@ 时 %@ 分 到 %@ 时 %@ 分",
                          tmpShour, tmpSminute, tmpEhour, tmpEminute];
    
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:time];
        
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, time.length)];
        
        //时间
//        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Hiragino Sans GB" size:19.0] range:NSMakeRange(0, 2)];
//
//        //"时"
//        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Hiragino Sans GB" size:15.0] range:NSMakeRange(2, 1)];
//        
//        //时间
//        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Hiragino Sans GB" size:19.0] range:NSMakeRange(3, 2)];
//        
//        //"分"
//        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Hiragino Sans GB" size:15.0] range:NSMakeRange(5, 1)];
//        
//        //"到"
//        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Hiragino Sans GB" size:15.0] range:NSMakeRange(7, 1)];
//        
//        //时间
//        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Hiragino Sans GB" size:19.0] range:NSMakeRange(9, 2)];
//        
//        //"时"
//        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Hiragino Sans GB" size:15.0] range:NSMakeRange(11, 1)];
//        
//        //时间
//        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Hiragino Sans GB" size:19.0] range:NSMakeRange(12, 2)];
//        
//        //"分"
//        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Hiragino Sans GB" size:15.0] range:NSMakeRange(14, 1)];
        
        cell.timeLabel.attributedText = str;
    
        return cell;
    }
    else if (indexPath.row == 1)
    {
        self.cellSecond = [ExamSignUpTableViewCellSecond cellWithTableView:tableView];
        
        self.cellSecond.adress.text = [UserDataSingleton shareInstance].place;
        
        return self.cellSecond;
    }
    else if (indexPath.row == 2)
    {
        ExamSignUpTableViewCellThird *cell = [ExamSignUpTableViewCellThird cellWithTableView:tableView];
        cell.courseDeletage = self;
        cell.lession.text = [UserDataSingleton shareInstance].examCourse;
        return cell;
    }
    else if (indexPath.row == 3)
    {
        ExamSignUpTableViewCellFouth *cell = [ExamSignUpTableViewCellFouth cellWithTableView:tableView];
        cell.levelDelegate = self;
        
        if ([[UserDataSingleton shareInstance].musicBase isEqualToString:
             [UserDataSingleton shareInstance].examCourseId])
        {
            NSString *level = [UserDataSingleton shareInstance].examLevel;
            if ([level isEqualToString:@"1"])
            {
                cell.level.text = @"初级";
            }
            else if ([level isEqualToString:@"2"])
            {
                cell.level.text = @"中级";
            }
            else if ([level isEqualToString:@"3"])
            {
                cell.level.text = @"高级";
            }
        }
        else
        {
            cell.level.text = [UserDataSingleton shareInstance].examLevel;
        }
        return cell;
    }
    else if (indexPath.row == 4)
    {
        ExamSignUpTableViewCellFirth *cell = [ExamSignUpTableViewCellFirth cellWithTableView:tableView];
        cell.examGuideDelegate = self;
        return cell;
    }
    else if (indexPath.row == 5)
    {
        ExamSignUpTableViewCellSix *cell = [ExamSignUpTableViewCellSix cellWithTableView:tableView];
        
        cell.delegate = self;
        cell.costLabel.text = self.cost;
        
        return cell;
    }
    
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    
    return cell;
}

-(void)configNavigationItem
{
    self.navigationItem.title = @"报名";
    [self.navigationController.navigationBar setTranslucent:NO];
    
    [self.navigationController.navigationBar
     setBackgroundImage:[UIImage imageNamed:@"bg_navigation_bar"]
     forBarMetrics:UIBarMetricsDefault];
    
    //status bar的字体为白色
    //导航栏的背景色是黑色
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],UITextAttributeTextShadowColor,[UIFont fontWithName:@"Hiragino Sans GB" size:18.0],NSFontAttributeName,nil]];
    
    [self configNavigationBackAction:^{
        
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

-(void)useMethodToFindBlackLineAndHind
{
    UIImageView* blackLineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    
    blackLineImageView.hidden = YES;
}

-(void)signUp
{
    UserDataSingleton *userData = [UserDataSingleton shareInstance];
    if(![userData isLogin])
    {
        [userData pleaseLogin:self];
        return;
    }
    
    [MBProgressHUD showMessage:@"正在加载"];
    
    QXNetWorkTool *QXnetWorkTool = [[QXNetWorkTool alloc]init];
    
    QXnetWorkTool.QXNetWorkDelegate = self;
    
    NSString *time = [self getExamTime];
    NSString *yearAndMonth = [time substringWithRange:NSMakeRange(0, 10)];
    
    [QXnetWorkTool GetDataFromServerWithPostMethodandInterfaceName:SigeUpInterface andParameter:
     @[@{@"user_id":userData.user_id,
         @"exam_site_id":userData.placeId,
         @"major_id":userData.examCourseId,
         @"grade_num":userData.examLevel,
         @"exam_time":time,
         @"site_date":yearAndMonth,
         @"site_time_id":userData.timeSecionId,
         @"cost":self.cost
         }] tag:2];
}

-(NSString *)getExamTime
{
    NSString *year = [UserDataSingleton shareInstance].year;
    NSString *tmpMonth = [UserDataSingleton shareInstance].month;
    NSString *tmpDay = [UserDataSingleton shareInstance].day;
    NSString *tmpShour = [UserDataSingleton shareInstance].sHour;
    NSString *tmpSminute = [UserDataSingleton shareInstance].sMinute;
    
    if ([tmpMonth intValue] < 10)
    {
        tmpMonth = [NSString stringWithFormat:@"0%@",
                    [UserDataSingleton shareInstance].month];
    }
    
    if ([tmpDay intValue] < 10)
    {
        tmpDay = [NSString stringWithFormat:@"0%@",
                  [UserDataSingleton shareInstance].day];
    }
    
    if ([tmpShour intValue] < 10)
    {
        tmpShour = [NSString stringWithFormat:@"0%@",
                    [UserDataSingleton shareInstance].sHour];
    }
    
    if ([tmpSminute intValue] < 10)
    {
        tmpSminute = [NSString stringWithFormat:@"0%@",
                      [UserDataSingleton shareInstance].sMinute];
    }
    
    //时间
    NSString *time = [NSString stringWithFormat:@"%@-%@-%@ %@:%@:%@",
                      year, tmpMonth, tmpDay, tmpShour, tmpSminute, @"00"];
    
    return time;
}

//-(void)didSelectLevel
//{
//    SelectExamLevelView *obj = [SelectExamLevelView create];
//    obj.view.backgroundColor = [UIColor clearColor];
//    obj.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
//    obj.modalPresentationStyle = UIModalPresentationOverCurrentContext;
//    obj.definesPresentationContext = YES;
//    obj.selectExamlevelDelegate = self;
//    
//    [obj setLevelData:self.data];
//    [self presentViewController:obj animated:YES completion:nil];
//}
//
//-(void)setLevel:(NSString *)str
//{
//    self.examLevel = str;
//    
//    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:3 inSection:0];
//    NSArray *indexArray = [NSArray arrayWithObject:indexPath];
//    
//    [self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationAutomatic];
//}
//
//-(void)didSelectCourse
//{
//    ExamCourseViewController *obj = [ExamCourseViewController create];
//    obj.selectExamCourseDelegate = self;
//    [self.navigationController pushViewController:obj animated:YES];
//}
//
//-(void)setSelectExamCourse:(NSString *)str
//{
//    self.examCourse = str;
//    
//    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2
//                                                inSection:0];
//    NSArray *indexArray = [NSArray arrayWithObject:indexPath];
//    
//    [self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationAutomatic];
//}
//

-(void)showExamGuide
{
    ExamGuideViewController *obj = [ExamGuideViewController create];
    
    [self.navigationController pushViewController:obj animated:YES];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self configNavigationItem];

    [self useMethodToFindBlackLineAndHind];
}

-(void)viewWillDisappear:(BOOL)animated
{

}


@end









